'use strict';

var valueToString2TypeMap = {
  '[object Boolean]': 'boolean',
  '[object Number]': 'number',
  '[object String]': 'string',
  '[object Function]': 'function',
  '[object Array]': 'array',
  '[object Date]': 'date',
  '[object RegExp]': 'regExp',
  '[object Undefined]': 'undefined',
  '[object Null]': 'null',
  '[object Object]': 'object',
}

export function getType(obj) {
  var toString = Object.prototype.toString
  return valueToString2TypeMap[toString.call(obj)]
}

export function deepClone(src, tar) {
  var type = getType(src)
  if (type === 'array') {
    tar = tar || []
    src.forEach(item => tar.push(deepClone(item)))
    return tar
  } else if (type === 'object') {
    tar = tar || {}
    for (const key in src) {
      tar[key] = deepClone(src[key])
    }
    return tar
  } else {
    return src
  }
}
